package com.zookeeper.basic.one;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import sun.util.resources.cldr.kk.TimeZoneNames_kk;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
 * Created by 58 on 2016-7-15.
 */
public class ZkConstructSampleWithSId implements Watcher{
    private static CountDownLatch countDownLatch = new CountDownLatch(1);
    public void process(WatchedEvent event) {
        System.out.println("Receive watched event : "+event);
        if(Event.KeeperState.SyncConnected == event.getState()){
            countDownLatch.countDown();
        }
    }

    public static void main(String[] args) throws InterruptedException, IOException {


        ZooKeeper zooKeeper = new ZooKeeper("127.0.0.1:2181",5000,new ZkConstructSampleWithSId());
        countDownLatch.await();
        long sessionId = zooKeeper.getSessionId();
        byte[] passwd = zooKeeper.getSessionPasswd();
        System.out.println("sessionid = "+sessionId+"----"+passwd.toString());
        ZooKeeper zooKeeper1 = new ZooKeeper("127.0.0.1:2181",5000,new ZkConstructSampleWithSId(),1l,"test".getBytes());
        zooKeeper1 = new ZooKeeper("127.0.0.1:2181",5000,new ZkConstructSampleWithSId(),sessionId,passwd);
        Thread.sleep(Integer.MAX_VALUE);
    }

}
